package com.example.peibei.ui.fragment;

import android.app.AlertDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.dingtao.common.bean.Order;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDFragment;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.SPUtils;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.base.SpConstant;
import com.example.peibei.service.presenter.DelateBillPresenter;
import com.example.peibei.service.presenter.HideOrderPresenter;
import com.example.peibei.service.presenter.ISendBillPresenter;
import com.example.peibei.ui.activity.EditOrderActivity;
import com.example.peibei.ui.activity.OrderDetailActivity;
import com.example.peibei.ui.adapter.MyBillListAdapter;
import com.example.peibei.ui.adapter.MyHideBillListAdapter;
import com.example.peibei.ui.weight.CenterDialog;
import com.example.peibei.ui.weight.EmptyRecyclerView;
import com.tencent.qcloud.tuicore.util.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class MySendBillHideFragment extends WDFragment {
    @BindView(R.id.order_list)
    EmptyRecyclerView order_list;
    @BindView(R.id.empty_view)
    LinearLayout empty_view;

    List<Order.RecordsDTO> orders;
    private MyHideBillListAdapter homeOrderListAdapter;
    private ISendBillPresenter iSendBillPresenter;
    private SPUtils spUtils;
    private String token;
    private DelateBillPresenter delateBillPresenter;
    private HideOrderPresenter hideOrderPresenter;

    @Override
    public String getPageName() {
        return null;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_my_send_bill_normal;
    }

    @Override
    protected void initView() {
        spUtils = new SPUtils(getActivity(), SpConstant.USER_TOKEN);
        token = spUtils.getString(SpConstant.USER_TOKEN);
        orders = new ArrayList<>();
        order_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        homeOrderListAdapter = new MyHideBillListAdapter(orders);
        order_list.setAdapter(homeOrderListAdapter);
        order_list.setEmptyView(empty_view);

        iSendBillPresenter = new ISendBillPresenter(new OrderCall());
        delateBillPresenter = new DelateBillPresenter(new DelateCall());
        hideOrderPresenter = new HideOrderPresenter(new HideCall());
        homeOrderListAdapter.setOnItemClickListener((adapter, view, position) -> {
            Order.RecordsDTO order = (Order.RecordsDTO) adapter.getData().get(position);
            String publishOrderId = order.getPublishOrderId();
            Bundle bundle = new Bundle();
            bundle.putString("publishOrderId", publishOrderId);
            bundle.putString("type", "0");
            intent(OrderDetailActivity.class, bundle);
        });
        homeOrderListAdapter.addChildClickViewIds(R.id.ll_delate, R.id.ll_edit, R.id.ll_off, R.id.ll_edit_again);
        homeOrderListAdapter.setOnItemChildClickListener(new OnItemChildClickListener() {
            @Override
            public void onItemChildClick(@NonNull BaseQuickAdapter adapter, @NonNull View view, int position) {
                Order.RecordsDTO data = (Order.RecordsDTO) adapter.getData().get(position);
                if (view.getId() == R.id.ll_delate) {
                    showDelateDialog(data.getPublishOrderId());
                } else if (view.getId() == R.id.ll_edit || view.getId() == R.id.ll_edit_again) {
                    Bundle bundle = new Bundle();
                    bundle.putString("orderId", data.getPublishOrderId());
                    intent(EditOrderActivity.class, bundle);
                } else if (view.getId() == R.id.ll_off) {
                    showDeletelDialog(data.getPublishOrderId());
                }
            }
        });
    }

    private void showDeletelDialog(String orderId) {
        View dialogView = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_center_view, null);
        CenterDialog bottomDialog = new CenterDialog(getActivity());
        bottomDialog.setContentView(dialogView);
        bottomDialog.setCanceledOnTouchOutside(true);
        bottomDialog.show();

        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        tv_title.setText("确定下架此订单吗？");
        tv_cancel.setOnClickListener(v -> bottomDialog.dismiss());
        tv_sure.setOnClickListener(v -> {
            hideOrderPresenter.reqeust("Bearer " + token, orderId);
            bottomDialog.dismiss();
        });
    }

    private void showDelateDialog(String orderId) {
        final AlertDialog.Builder normalDialog =
                new AlertDialog.Builder(getActivity());
        final View dialogView = LayoutInflater.from(getActivity())
                .inflate(R.layout.dialog_bind_account, null);
        normalDialog.setView(dialogView);
        TextView tv_name = dialogView.findViewById(R.id.tv_name);
        TextView tv_title = dialogView.findViewById(R.id.tv_title);
        TextView tv_cancel = dialogView.findViewById(R.id.tv_cancel);
        TextView tv_sure = dialogView.findViewById(R.id.tv_sure);
        tv_name.setText("提示");
        tv_title.setText("确定删除当前订单吗？");
        tv_sure.setText("删除");
        AlertDialog alertDialog = normalDialog.create();
        tv_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
            }
        });
        tv_sure.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.cancel();
                delateBillPresenter.reqeust("Bearer " + token, orderId);
            }
        });
        alertDialog.show();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume
            iSendBillPresenter.reqeust(true, "Bearer " + token, 2);
        } 
    }

    @Override
    public void onResume() {
        super.onResume();
        iSendBillPresenter.reqeust(true, "Bearer " + token, 2);
    }


    class HideCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("已下架");
            iSendBillPresenter.reqeust(true, "Bearer " + token, 2);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class OrderCall implements DataCall<Order> {
        @Override
        public void success(Order data, Object... args) {
            //添加列表并刷新
            if (iSendBillPresenter.getPage() == 1) {
                orders.clear();
            }
            homeOrderListAdapter.setNewInstance(data.getRecords());
            homeOrderListAdapter.notifyDataSetChanged();
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }

    class DelateCall implements DataCall<String> {
        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("删除成功");
            iSendBillPresenter.reqeust(true, "Bearer " + token, 2);
        }

        @Override
        public void fail(ApiException e, Object... args) {

        }
    }
}
